require 'fileutils'

class PidFile
  attr_reader :pid, :filename
  def initialize(filename)
    @filename = filename
    @pid = IO.read(filename).to_i
  end

  def kill
    Process.kill('TERM', pid) rescue nil
    cleanup
  end

  def cleanup
    FileUtils.rm(filename) rescue nil
  end
end

def with_pidfiles(glob="log/*.pid")
  pfs = Dir[glob].map{ |filename| PidFile.new(filename) }
  yield(pfs) if pfs.size > 0
end
